package com.linewow.xhyy.nihaonew.control;

import android.content.Context;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;

import com.linewow.xhyy.nihaonew.R;
import com.linewow.xhyy.nihaonew.fragment.SearchImageFragment;
import com.linewow.xhyy.nihaonew.fragment.SearchResultFragment;
import com.linewow.xhyy.nihaonew.inter.SureCall;
import com.linewow.xhyy.nihaonew.inter.FinishCall;

/**
 * Created by LXR on 2016/8/31.
 */
public class SearchBeginDataControl {
    private FragmentManager fm;
    private SearchImageFragment imageFragment;
    private SearchResultFragment resultFragment;
    private Context context;
    private boolean flag; //控制梯度
    private FinishCall call;
    private SureCall sureCall;

    public SearchBeginDataControl(FragmentManager fm,FinishCall call) {
        this.fm = fm;
        this.call=call;
        initFragment();
    }

    public void setSureCall(SureCall sureCall) {
        this.sureCall = sureCall;
    }

    private void initFragment() {
        FragmentTransaction ft=fm.beginTransaction();
        imageFragment=new SearchImageFragment();
        resultFragment=new SearchResultFragment();
        ft.add(R.id.search_begin_main,imageFragment);
        ft.add(R.id.search_begin_main,resultFragment);
        ft.hide(resultFragment);
        ft.commit();
    }



    private void goneFragment() {
        FragmentTransaction ft=fm.beginTransaction();
        if(imageFragment!=null){
            ft.hide(imageFragment);
        }
        if(resultFragment!=null){
            ft.hide(resultFragment);
        }
        ft.commit();
    }

    public void resultShow(View view){
        flag=true;
        goneFragment();
        FragmentTransaction ft=fm.beginTransaction();
        ft.show(resultFragment);
        ft.commit();
        sureCall.sureFinish();

    }

    public void imageShow(View view){
        if(flag){
            flag=false;
            goneFragment();
            FragmentTransaction ft=fm.beginTransaction();
            ft.show(imageFragment);
            ft.commit();
        }else{
            call.actFinish();
        }
    }


}
